﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.UI.WebControls;
using System.Data.Common;

namespace AthleteService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“Service1”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs，然后开始调试。
    public class Service1 : IService1
    {
        private IService1Callback callback; // 指向客户端的回调通道，服务使用该回调通道通过回调接口将消息发送回客户端
        public Service1()
        {
            //
            OperationContext context = OperationContext.Current; // 获取当前OperationContext
            callback = context.GetCallbackChannel<IService1Callback>(); // 通过OperationContext获得客户端回调通道
            //
        }

        public void EventDelete(string selectedItem)
        {
            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("delete from eevent where eno='{0}'", selectedItem));
            }
            catch
            {

            }
            finally
            {
                var dt = eventdt();
                callback.ShowResults(dt, res);
            }
        }

        public void EventInsert(string[] selectedItem)
        {
            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("insert into eevent values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", selectedItem[0], selectedItem[1], selectedItem[2], selectedItem[3], selectedItem[4], selectedItem[5], selectedItem[6]));
            }
            catch
            {

            }
            finally
            {
                var dt = eventdt();
                callback.ShowResults(dt, res);
            }
        }

        public void EventSelectAll()
        {
            var dt = eventdt();
            callback.ShowResults(dt, 1);
        }

        public DataTable eventdt()
        {
            var dt = DBHelper.GetDataSet("select eno '比赛编号', ename '比赛名称', etime '比赛时间', fno '项目编号', jno '裁判编号', sno '场地编号', esex '性别组别' from eevent");
            return dt;
        }

        public void EventSelectKeyword(string keyword)
        {
            var dt = DBHelper.GetDataSet(String.Format("select eno '比赛编号', ename '比赛名称', etime '比赛时间', fno '项目编号', jno '裁判编号', sno '场地编号', esex '性别组别' from eevent where eno like '%{0}%' or ename like '%{0}%' or etime like '%{0}%' or fno like '%{0}%' or jno like '%{0}%' or sno like '%{0}%' or esex like '%{0}%'", keyword));
            callback.ShowResults(dt, 1);
        }

        public DataTable Update(string UserName)
        {
            DBConnection3 conn = new DBConnection3("eno", UserName);
            DataTable dataset = conn.Select();
            if (dataset == null)
            {
                return null;
            }
            return dataset;

        }

        public bool Delete(string result, string UserName)
        {
            try
            {

                string strCon = "server=.;database=sportmeet;user=sa;pwd=Jackson.yi11.28";
                SqlConnection con = new SqlConnection(strCon);
                string strSql = "delete from signup where ano= '" + UserName + "'and eno='" + result + "'";
                SqlCommand cmd = new SqlCommand(strSql, con);
                con.Open();
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }

                con.Close();

            }
            catch
            {
                return false;
            }

        }
        public bool Insert(string[] selectedItem, string result, string UserName)
        {
            try
            {

                string strCon = "server=.;database=sportmeet;user=sa;pwd=Jackson.yi11.28";
                SqlConnection con = new SqlConnection(strCon);
                string strSql = "insert into signup(Ano,eno) values( '" + UserName + "','" + result + "')";
                SqlCommand cmd = new SqlCommand(strSql, con);
                con.Open();
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }

                con.Close();

            }
            catch
            {
                return false;
            }
        }

        public DataTable subjectdt()
        {
            var dt = DBHelper.GetDataSet("select Fno '项目编号', fname '项目名称' from form");
            return dt;
        }
        public void SelectAll() // 实现服务接口中的方法
        {
            //var dt = DBHelper.GetDataSet("select Fno '项目编号', fname '项目名称' from form");
            var dt = subjectdt();
            callback.ShowResults(dt, 1); // 使用回调通道通过回调接口将消息发送回客户端
        }

        public void SelectKeyword(string keyword)
        {
            var dt = DBHelper.GetDataSet(String.Format("select Fno '项目编号', fname '项目名称' from form where Fno like '%{0}%' or fname like '%{0}%'", keyword));
            callback.ShowResults(dt, 1);
        }

        public void SubjectUpdate(string[] selectedItem)
        {

            int res = 0;
            try
            {
                res = DBHelper.GetExcuet(string.Format("delete from form where Fno='{0}'", selectedItem[0]));
                DBHelper.GetExcuet(string.Format("insert into form values ('{0}', '{1}')", selectedItem[0], selectedItem[1]));
            }
            catch
            {

            }
            finally
            {
                var dt = subjectdt();
                callback.ShowResults(dt, res);
            }
        }

    }

}

